package com.huangmb.anymark.app.api;

import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.huangmb.common.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/5/7 16:10
 */
public class CookiesSupportRequest extends StringRequest {
    private Map<String,String> header = new HashMap<>(1);

    public CookiesSupportRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }

    public CookiesSupportRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        Map<String, String> headers = response.headers;
        String cookie = headers.get("Set-Cookie");
        if(StringUtils.isNotBlank(cookie)){
            CookieManager.setCookie(cookie);
        }
        return super.parseNetworkResponse(response);
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        String cookie = CookieManager.getCookie();
        if(StringUtils.isNotBlank(cookie)){
            header.put("Cookie",cookie);
        }
        return header;
    }
}
